<?php
	const ROLE_ADMIN = 1;
	const ROLE_DOMAIN_USER = 2;
	const ROLE_USER = 3;
	
	$model = new Admin_Model_Entity();
	$entity_roles = $model->listAll()->toArray();
	
	$entity_roleCollection = array();
	foreach($entity_roles as $er)
	{
		if(! isset($entity_roleCollection[$er->role_id])){
			$entity_roleCollection[$er->role_id] = array();
		}
		
		$entity_roleCollection[$er->role_id][$er->entity_name] = array(
			'view' => $er->view,
			'insert' => $er->insert,
			'update' => $er->update,
			'delete' => $er->delete,
		);
	}
	
	/**
	 * Check if user has role
	 * @param int role_id
	 * @return bool true if current user has request role
	 */
	public function hasRole($role_id)
	{
		if(! isset($_SESSION['user'])){
			return false;
		}
		
		$current_user = $_SESSION['user'];
		return $current_user-> == $role;
	}
	
	/**
	 * Check if current user has request permission in entity
	 * @param string entity_name
	 * @param string permission
	 * @return bool<br/>
	 *   <i>false</i> if not exist role for current user or not exist request entity_name<br/>
	 *   <i>false</i> else return role's permission in entity
	 */
	public function hasAccess($entity_name, $permission)
	{
		if(! isset($_SESSION['user'])){
			return false;
		}
		$current_user = $_SESSION['user'];
		
		if(!isset($entity_roleCollection[$current_user->role_id][$entity_name]['$permission']))
		{
			return false;
		}
		return $entity_roleCollection[$current_user->role_id][$entity_name]['$permission'];
	}
?>